import debugMan from '../../lib/debugMan';
const paginationHeight = 56;
function calcTableScroll(containerEl, prefixCls, callback) {
    if (!containerEl) {
        callback({ scrollAble: false })
        return;
    }
    /**
    * * antd table中，table的header和pagination渲染的时间不可控，因为antd-table没有接口提供给调用方，用于获取内部的header等对象
    * * 所以采用了循环监测的方式获取table内的相关对象，对象为table的header对象、table的pagination对象
    * * 为了实现动态的fix header效果，必须获得真实的container，header和pagination的DOM对象，才能准确算出table body中的scroll高度
    * * 新增功能：当table中的内容达不到需要scroll的效果时，不采用表头冻结的方式
    */
    let getHeaderPromise = new Promise((r, j) => {
        getDomLoop(() => {
            let target = containerEl.getElementsByClassName(`${prefixCls}-table-thead`)?.[0];
            return { status: target ? 'success' : 'fail', value: target };
        }, 10, (resp) => {
            resp.status === 'success' ? r(resp.value) : j()
        })
    })
    let getBodyPromise = new Promise((r, j) => {
        getDomLoop(() => {
            let target = containerEl.getElementsByClassName(`${prefixCls}-table-tbody`)?.[0];
            return { status: target ? 'success' : 'fail', value: target };
        }, 10, (resp) => {
            resp.status === 'success' ? r(resp.value) : j()
        })
    })
    let getPaginationPromise = new Promise((r, j) => {
        getDomLoop(() => {
            let target = containerEl.getElementsByClassName(`${prefixCls}-pagination`)?.[0];
            return { status: target ? 'success' : 'fail', value: target };
        }, 50, (resp) => {
            resp.status === 'success' ? r(resp.value) : j()
        })
    })
    Promise.allSettled([getHeaderPromise, getBodyPromise, getPaginationPromise]).then(values => {
        let headerEl, bodyEl, paginationEl;
        values.forEach((d, i) => {
            if (d.status === 'fulfilled') {
                switch (i) {
                    case 0:
                        headerEl = d.value;
                        break;
                    case 1:
                        bodyEl = d.value;
                        break;
                    case 2:
                        paginationEl = d.value;
                        break;
                    default:
                        break;
                }
            }
        })
        if (headerEl) debugMan.console('Catch the table header');
        if (bodyEl) debugMan.console('Catch the table body');
        if (paginationEl) debugMan.console('Catch the table pagination');
        // * 表头和容器肯定会有
        let bodyGap = containerEl.clientHeight - headerEl.clientHeight;
        if (paginationEl) {
            let paginationStyles = paginationEl ? window.getComputedStyle(paginationEl) : {};
            let marginHeight = Number(paginationStyles?.marginTop?.replace('px', '')) + Number(paginationStyles?.marginBottom?.replace('px', ''));
            bodyGap += - paginationEl.clientHeight - marginHeight;
        } else {
            //* 如果分页组件找不到，则使用一个固定页面组件高度（根据antd默认的分页器高度所得）
            bodyGap += -paginationHeight;
        }
        if (bodyGap >= bodyEl.scrollHeight) {
            debugMan.console(`tableGap is less than table body scroll height;tableGap is ${bodyGap},scrollHeight is${bodyEl.scrollHeight}`);
        }
        callback({ scrollAble: true, value: bodyGap });

    }).catch((e) => {
        console.warn(e);
    });
}
function getDomLoop(iterateFn, count = 10, callback) {
    setTimeout(() => {
        let resp = iterateFn();
        if (resp.status === 'success' || count === 0) {
            callback(resp)
        } else if (count > 0) {
            getDomLoop(iterateFn, count - 1, callback,)
        }
    }, 100);
}
export default calcTableScroll;